package com.ruoyi.hr.controller.hr.job;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.Companys;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.CompanysService;
import com.ruoyi.hr.service.jobadmin.JobAdminService;
import com.ruoyi.hr.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 岗位管理控制层
 */
@Controller
@RequestMapping("/job/admin")
public class JobAdminController extends BaseController {

    @Autowired
    AuthorityService authorityService;

    @Autowired
    JobAdminService jobAdminService;

    @Autowired
    CompanysService companysService;

    private String prefix = "hr/jobadmin";

    /**
     * 新增登记
     */
    @GetMapping("/jobAdd")
    public String jobAdd() {
        return prefix + "/jobadd/jobAdd";
    }

    /**
     * 新增页面跳转
     *
     * @param mmap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<Companys> companys = companysService.findAllCompanys(null);
        mmap.put("companys", companys);
        //取所有有效部门
        mmap.put("dep", jobAdminService.findYxJob());
        return prefix + "/jobadd/add";
    }

    /**
     * 新增操作
     *
     * @param map
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map map) {
        return toAjax(jobAdminService.insertJob(map));
    }

    @GetMapping("/editAdd/{id}")
    public String editAdd(@PathVariable("id") String id, ModelMap mmap) {
        Map map = jobAdminService.findJobAdd(id);
        mmap.put("job", map);
        mmap.put("dep", jobAdminService.findYxJob());
        List<Companys> companys = companysService.findAllCompanys(null);
        mmap.put("companys", companys);
        if (map.get("compId") != null) {
            mmap.put("xsDep", jobAdminService.findXsDep(map.get("compId").toString()));
        }
        return prefix + "/jobadd/edit";
    }

    @RequestMapping("/addEdit")
    @ResponseBody
    public AjaxResult addEdit(@RequestParam Map map) {
        return toAjax(jobAdminService.updateJobAdd(map));
    }


    @RequestMapping("/findJobList")
    @ResponseBody
    public TableDataInfo findJobList(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        return getDataTable(jobAdminService.findJobList(map, authority));
    }

    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String ids) {
        return toAjax(jobAdminService.delete(ids));
    }

    @PostMapping("/procedure")
    @ResponseBody
    public BaseResult procedure(String allId, String procedure) {
        return jobAdminService.procedure(allId, procedure);
    }

    /**
     * 变更登记
     */
    @GetMapping("/jobEdit")
    public String jobEdit() {
        return prefix + "/jobedit/jobEdit";
    }

    /**
     * 跳转添加修改岗位界面
     */
    @GetMapping("/addEditJob")
    public String addEditJob() {
        return prefix + "/jobedit/addEditJob";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Map map = jobAdminService.findJobAdd(id);
        mmap.put("job", map);
        mmap.put("dep", jobAdminService.findYxJob());
        List<Companys> companys = companysService.findAllCompanys(null);
        mmap.put("companys", companys);
        if (map.get("compId") != null) {
            mmap.put("xsDep", jobAdminService.findXsDep(map.get("compId").toString()));
        }
        return prefix + "/jobedit/edit";
    }

    @RequestMapping("/editEdit")
    @ResponseBody
    public AjaxResult editEdit(@RequestParam Map map) {
        return toAjax(jobAdminService.updateJobEdit(map));
    }

    /**
     * 获取修改岗位数据
     */
    @RequestMapping("/findEditJobList")
    @ResponseBody
    public TableDataInfo findEditJobList(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        return getDataTable(jobAdminService.findEditJobList(map, authority));
    }

    @PostMapping("/addEditJob")
    @ResponseBody
    public BaseResult addEditJob(String allJob, String type) {
        return jobAdminService.addEditJob(allJob, type);
    }


    /**
     * 失效登记
     */
    @GetMapping("/jobInvalid")
    public String jobInvalid() {
        return prefix + "/jobinvalid/jobInvalid";
    }

    /**
     * 跳转添加修改岗位界面
     */
    @GetMapping("/addInvalidJob")
    public String addInvalidJob() {
        return prefix + "/jobinvalid/addInvalidJob";
    }

    @GetMapping("/editInvalid/{id}")
    public String editDepInvalid(@PathVariable("id") String id, ModelMap mmap) {
        Map map = jobAdminService.findJobAdd(id);
        mmap.put("job", map);
        return prefix + "/jobinvalid/edit";
    }

    @RequestMapping("/editInvalid")
    @ResponseBody
    public AjaxResult editDepInvalid(@RequestParam Map map) {
        return toAjax(jobAdminService.editDepInvalid(map));
    }


    /**
     * 跳转历史页面
     */
    @GetMapping("/history")
    public String history(ModelMap mmap) {
        return prefix + "/jobhistory/historyJobAdmin";
    }

    /**
     * 获取岗位历史数据
     */
    @RequestMapping("/findHistoryJobList")
    @ResponseBody
    public TableDataInfo findHistoryJobList(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        return getDataTable(jobAdminService.findHistoryJobList(map, authority));
    }


    @GetMapping("/jobYx")
    public String jobYx() {
        return prefix + "/jobinfo/jobinfoYx";
    }

    @GetMapping("/jobSx")
    public String jobS() {
        return prefix + "/jobinfo/jobinfoSx";
    }


    @RequestMapping("/jobInfoList")
    @ResponseBody
    public TableDataInfo jobInfoList(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        return getDataTable(jobAdminService.jobInfoList(map, authority));
    }
}
